const fs = require('fs');
global["__DEV__"] =  true
class App {
    config: any
    logger: any
    gameName: string
    env: string
    version: string
    minVersion: string
    minMax: number
    minMin: number
    arenaFork: any
    csFork: any
    serverState: number //服务器状态 0 正常, 1维护中
    stateText: string
    playerSessionForks: any
    playerSessionForksByID: any
    forkmessageCb: any
    messageID: number
    forkID: number
    type: string
    limitWord: any = {}
    limitWordArray: any = []
    limitWordStrs: string = ''
    constructor() {
        this.config = null
        this.logger = null
        this.env = ''
        this.version = '1.1'
        this.minVersion = '1.1'
        this.minMax = 1
        this.minMin = 1
        this.arenaFork = null
        this.csFork = null
        this.serverState = 0
        this.stateText = "服务器维护中"
        this.playerSessionForks = []
        this.playerSessionForksByID = {}
        this.messageID = 0
        this.forkmessageCb = {}
        this.type = null
        this.init()
    }

    init() {
        const filePaths = ['key', '暴恐词库', '反动词库', '民生词库', '其他词库', '色情词库', '贪腐词库'];
        filePaths.forEach(filePath => {
            // 读取文件内容
            fs.readFile("./src/limitWord/" + filePath + ".txt", 'utf8', (err, data) => {
                if (err) {
                    console.error('读取文件失败:', err);
                    return;
                }

                // 按照 | 分割内容
                const splitContent = data.split('|');
                splitContent.forEach(word => {
                    this.limitWord[word] = 1
                })
                splitContent.forEach(word => {
                    if (word.length < 8) {
                        this.limitWordArray.push(word)
                    }
                })
            });
        })

    }

    // 替换函数
    replaceSensitiveWords(inputText) {
        let resultText = inputText;
        // 替换文本中的敏感词汇
        if (this.limitWordStrs == "" || !this.limitWordStrs) {
            this.limitWordStrs = this.limitWordArray.join('|')
        }
        // const pattern = new RegExp(`\\b(${this.limitWordStrs})\\b`, 'gi');
        this.limitWordArray.forEach(word => {
            const regex = new RegExp(word, 'g'); // 创建全局匹配的正则表达式
            const replacement = '*'.repeat(word.length); // 替换为 *
            resultText = resultText.replace(regex, replacement);
        });
        return resultText;
    }


    sendMessage(fork, url, sessionKey, data): Promise<any> {
        this.messageID += 1
        fork.send({ messageID: this.messageID, url, sessionKey, data: data })
        return new Promise(async resolve => {
            this.forkmessageCb[this.messageID] = (msg) => {
                resolve(msg)
            }
        })
    }
}

let app = new App()
export default app